package com.scau402.system1.repository.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scau402.common.core.domain.entity.SysUser;
import com.scau402.system1.domain.dto.WarehouseStorage;
import com.scau402.system1.domain.dto.WmsCheckBoundOrderDTO;
import com.scau402.system1.domain.po.WmsAdjustBoundOrderDetailPO;
import com.scau402.system1.domain.po.WmsAdjustBoundOrderPO;
import com.scau402.system1.domain.po.WmsAuditOrderPO;
import com.scau402.system1.domain.query.WmsAdjustBoundOrderQuery;
import com.scau402.system1.domain.vo.WmsAdjustBoundOrderVO;
import com.scau402.system1.domain.vo.WmsAdjustEmployeeVO;
import com.scau402.system1.mapper.IWmsAdjustBoundOrderMapper;
import com.scau402.system1.repository.IWmsAdjustDetailRepository;
import com.scau402.system1.repository.IWmsAdjustOrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 库存调整单Repository实现类
 */
@Repository
@Slf4j
public class WmsAdjustOrderRepositoryImpl implements IWmsAdjustOrderRepository {

    @Autowired
    private IWmsAdjustBoundOrderMapper adjustOrderMapper;
    @Autowired
    private IWmsAdjustDetailRepository adjustDetailRepo;

    /**
     * 根据创建则获取库存调整单
     *
     * @param user  用户实体类
     * @param query 查询
     * @return 调整单列表
     */
    @Override
    public List<WmsAdjustBoundOrderVO> findOrderListByCreator(SysUser user, WmsAdjustBoundOrderQuery query) {
        query.setTenantId(user.getTenantId());
        return adjustOrderMapper.selectAdjustListByQuery(query);
    }

    /**
     * 保存一条库存调整单记录
     * @param adjustBoundOrderPO 库存调整单PO
     */
    @Override
    public void save(WmsAdjustBoundOrderPO adjustBoundOrderPO) {
        adjustOrderMapper.insert(adjustBoundOrderPO);
    }

    /**
     * 保存一条库存调整单记录，同时创建其详情信息
     * @param adjustBoundOrderPO 库存调整单PO
     * @param orderDetailList 库存调整单翡翠详情列表
     */
    @Override
    public void save(WmsAdjustBoundOrderPO adjustBoundOrderPO, List<WmsAdjustBoundOrderDetailPO> orderDetailList) {
        adjustOrderMapper.insert(adjustBoundOrderPO);
        adjustDetailRepo.saveList(orderDetailList);
    }

    /**
     * 通过审核单信息查找关联的库存调整单详情信息
     * @param audit 审核单PO
     * @return 库存调整单PO
     */
    @Override
    public WmsAdjustBoundOrderDetailPO findByAudit(WmsAuditOrderPO audit) {
        return adjustDetailRepo.findAdjustByAudit(audit);
    }

    /**
     * 通过租户id查询盘点单的调整人员名单
     * @param tenantId 租户id
     * @return 调整人员名单
     */
    @Override
    public List<WmsAdjustEmployeeVO> getAdjustEmployee(Long tenantId) {
        return adjustOrderMapper.getAdjustEmployee(tenantId);
    }

    @Override
    public List<String> getCheckOrderCode(Long tenantId) {
        return adjustOrderMapper.getCheckOrderCode(tenantId);
    }

    /**
     * 检查是否存在正在审核的仓库/仓位
     * @param target
     */
    @Override
    public Long checkIsAuditing(WarehouseStorage target) {
        return Optional.ofNullable(adjustOrderMapper.checkIsAuditing(target)).orElse(-1L);
    }
}
